package com.andy.beef.ui.main.activity

import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.afollestad.materialdialogs.MaterialDialog
import com.alibaba.android.arouter.facade.annotation.Route
import com.andy.beef.R
import com.andy.beef.databinding.MainActivityEnterInfoListBinding
import com.andy.beef.ui.main.adapter.EnterInfoListAdapter
import com.andy.beef.ui.main.bean.EnterInfoBean
import com.andy.beef.ui.main.viewmodel.EnterInfoListViewModel
import com.andy.module.base.BaseVbPageListActivity
import com.andy.module.constant.RoutePath
import com.andy.module.ext.addToolBar
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import me.hgj.jetpackmvvm.ext.parseState

/**
 * @creator:LuoFan
 * @date:2022/6/8
 * @description: 入场信息列表
 */
@Route(path = RoutePath.ENTER_INFO_LIST)
class EnterInfoListActivity : BaseVbPageListActivity<EnterInfoListViewModel, MainActivityEnterInfoListBinding, EnterInfoBean>() {

    private var mAdapter = EnterInfoListAdapter()

    override fun createObserver() {
        super.createObserver()
        mViewModel.deleteRecordResult.observe(this) {
            parseState(it, {
                ToastUtils.showShort("删除成功")
                mViewBind.refreshLayout.autoRefresh()
            }, {
                ToastUtils.showLong(it.errorMsg)
            })
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mViewBind.root.addToolBar(title = "入场信息") {
            finish()
        }
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val bean = mAdapter.getItem(position)
            when (view.id) {
                R.id.content -> {

                }
                R.id.btn_del -> {
                    showDeleteDialog(bean)
                }
            }
        }
    }

    private fun showDeleteDialog(bean: EnterInfoBean) {
        val deleteDialog = MaterialDialog(this)
            .cancelable(true)
            .cancelOnTouchOutside(false)
            .cornerRadius(12f)
        deleteDialog.title(text = "提示")
            .message(text = "是否删除该入场记录")
            .positiveButton(text = "删除") {
                deleteDialog.dismiss()
                mViewModel.deleteRecord(bean)
            }
            .negativeButton(text = "取消")
        deleteDialog.show()
    }

    override fun getAdapter(): BaseQuickAdapter<EnterInfoBean, BaseViewHolder> {
        return mAdapter
    }

    override fun getRecyclerView(): RecyclerView {
        return mViewBind.recyclerView
    }

    override fun getRefreshLayout(): SmartRefreshLayout {
        return mViewBind.refreshLayout
    }
}